package com.wyzz.hr.bo.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IWorkFlowApproverBo;
import com.wyzz.hr.dao.IWorkFlowApproverDAO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.WorkFlowApprover;

public class WorkFlowApproverBo implements IWorkFlowApproverBo {

	public WorkFlowApproverBo() {
	}

	public List findItems(String clazz) {
		Class c = null;
		try {
			c = Class.forName(clazz);
		} catch (ClassNotFoundException e) {
			return new ArrayList(0);
		}
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(c);
		return workFlowApproverDAO.findByCriteria(detachedCriteria);
	}

	public IWorkFlowApproverDAO getWorkFlowApproverDAO() {
		return workFlowApproverDAO;
	}

	public void setWorkFlowApproverDAO(IWorkFlowApproverDAO workFlowApproverDAO) {
		this.workFlowApproverDAO = workFlowApproverDAO;
	}

	public List findAll() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(WorkFlowApprover.class);
		return workFlowApproverDAO.findByCriteria(detachedCriteria);
	}

	public void deleteWorkFlowApproverById(String id) {
		workFlowApproverDAO.deleteObject(WorkFlowApprover.class, id);
	}

	public String addWorkFlowApprover(WorkFlowApprover w) {
		DetachedCriteria dc = DetachedCriteria.forClass(WorkFlowApprover.class);
		dc
				.add(Restrictions.eq("workFlowApproverId", w
						.getWorkFlowApproverId()));
		dc.add(Restrictions.eq("workFlowApproverType", w
				.getWorkFlowApproverType()));
		List exist = workFlowApproverDAO.findByCriteria(dc);
		if (exist.size() > 0) {
			return "\u8BE5\u9879\u76EE\u7684\u5BA1\u6279\u6D41\u7A0B\u5DF2\u8BBE\u7F6E\uFF0C\u4E0D\u80FD\u91CD\u590D\u8BBE\u7F6E\uFF01";
		} else {
			workFlowApproverDAO.saveObject(w);
			return null;
		}
	}

	public String updateWorkFlowApprover(WorkFlowApprover w) {
		WorkFlowApprover dbWorkFlow = (WorkFlowApprover) workFlowApproverDAO
				.loadObject(WorkFlowApprover.class, w.getId(), null,
						new boolean[0]);
		if (dbWorkFlow == null) {
			return "\u60A8\u8981\u66F4\u65B0\u7684\u6570\u636E\u4E0D\u5B58\u5728\u6216\u5DF2\u7ECF\u88AB\u5220\u9664\uFF0C\u8BF7\u91CD\u8BD5\uFF01";
		} else {
			dbWorkFlow.setWorkFlowLimit(w.getWorkFlowLimit());
			dbWorkFlow.setWorkFlowApproverInd(w.getWorkFlowApproverInd());
			workFlowApproverDAO.updateObject(dbWorkFlow);
			return null;
		}
	}

	public List getManagers() {
		String hql = "select distinct empSupNo.id from Employee";
		List sups = workFlowApproverDAO.exeHqlList(hql);
		DetachedCriteria dc = DetachedCriteria.forClass(Employee.class);
		if (sups != null && sups.size() > 0)
			dc.add(Restrictions.in("id", sups.toArray()));
		dc.add(Restrictions.eq("empStatus", new Integer(1)));
		dc.addOrder(Order.asc("empName"));
		dc.setFetchMode("empDeptNo", FetchMode.JOIN);
		return workFlowApproverDAO.findByCriteria(dc);
	}

	public List getWorkFlowApprover(String workFlowApproverId, String type[]) {
		DetachedCriteria dc = DetachedCriteria.forClass(WorkFlowApprover.class);
		dc
				.add(Restrictions.eq(WorkFlowApprover.PROP_WF_ID,
						workFlowApproverId));
		dc.add(Restrictions.in(WorkFlowApprover.PROP_WF_TYPE, type));
		return workFlowApproverDAO.findByCriteria(dc);
	}

	private IWorkFlowApproverDAO workFlowApproverDAO;
}
